﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.Text.RegularExpressions;
using HtmlAgilityPack;
namespace UcAsp.Net.Extract.Plug.QQ
{
    public partial class UcAspSettingControl : UserControl
    {
        public UcAspSettingControl()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           /* string htmldocument = BLL.HttpWebClient.GetString("http://fashion.qq.com/c/trends_1.htm");
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(htmldocument);
            
            HtmlNodeCollection c = document.DocumentNode.SelectNodes("/div/li/div/a");
            foreach (HtmlNode n in c)
            {

                string url = n.Attributes["href"].Value;
                HtmlAgilityPack.HtmlDocument icoPack = new HtmlAgilityPack.HtmlDocument();
                icoPack.LoadHtml(n.InnerHtml);
                HtmlNode im = icoPack.DocumentNode.SelectSingleNode("/img");
                string img = im.Attributes["src"].Value;
                string title = im.Attributes["alt"].Value;

            }*/
            
            string url = "http://fashion.qq.com/a/20150328/007753.htm";
            if (!string.IsNullOrEmpty(txtURL.Text))
            {
                url = txtURL.Text;
            }
            string htmldocument = BLL.HttpWebClient.GetString(url);
            HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(htmldocument);
            string docmenthtml = document.DocumentNode.SelectSingleNode("/html/body/div/div/div/div/div/div/div[@id='Cnt-Main-Article-QQ']").InnerHtml;
            RegexOptions options = RegexOptions.None;
            Regex regex = new Regex(@"<script>[\w\W]*<!\[endif\]\-->|<a[^>]*>|</a>", options);
            string input = docmenthtml;
            string replacement = @"";
            string result = regex.Replace(input, replacement);

            HtmlNode titlenote = document.DocumentNode.SelectSingleNode("/html/body/div/div/div/div/div[@id='C-Main-Article-QQ']/div/h1");
            string title = titlenote.InnerText;

            HtmlNode descenote = document.DocumentNode.SelectSingleNode("/html/body/div/div/div/div/div/div/div[@id='Cnt-Main-Article-QQ']/p[@class='titdd-Article']");
            string desce = descenote.InnerText;
            string htmldesc=descenote.OuterHtml;
            result = result.Replace(htmldesc, "");

            
            
           // string str = document.DocumentNode.SelectSingleNode("").InnerHtml;
            //HtmlAgilityPack.HtmlDocument pagedocument = new HtmlAgilityPack.HtmlDocument();
           // pagedocument.LoadHtml(str);

            HtmlNodeCollection c = document.DocumentNode.SelectNodes("/html/body/div/div/div/div/div/div/div[@class='page-Article-QQ']/ul/li/a");
            foreach (HtmlNode n in c)
            {
                string pageurl = n.Attributes["href"].Value;
                string number = n.InnerText;
            }
             
            //content.LoadHtml(docmenthtml);

        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            tabControl1.SelectTab(tabPage2);


        }

        private void UcAspSettingControl_Load(object sender, EventArgs e)
        {
            List<Model.Config> list = BLL.Config.GetListConfig;
            lbConfig.DataSource = list;
            lbConfig.DisplayMember = "DefinedTitle";
            lbConfig.ValueMember = "DefinedName";
        }

        private void lbConfig_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListBox list = (ListBox)sender;
            Model.FilesConfig config = BLL.FilesConfig.GetFilesConfig(list.SelectedValue.ToString());
            txtFileName.Text = config.FileName;
            txtName.Text = config.Name;
            txtURL.Text = config.Url;
            txtURLReg.Text = config.txtURLReg;
            IconUrl.Text = config.IconUrl;
            txtContentReg.Text = config.ContentReg;
            txtImageReg.Text = config.ImageReg;
            txtTitleReg.Text = config.TitleReg;
            txtPageReg.Text = config.PageReg;
            txtrootPath.Text = config.RootPath;
            txtPageUrl.Text = config.PageUrl;
            txtPageNumber.Text = config.PageNumber.ToString();
            txtClearReg.Text = config.ClearReg;
            txtCategorieID.Text = config.CategorieID.ToString();
            txtDescReg.Text = config.DescReg;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Model.FilesConfig file = new Model.FilesConfig();
            file.FileName = txtFileName.Text;
            file.Name = txtName.Text;
            file.ContentReg = txtContentReg.Text;
            file.ImageReg = txtImageReg.Text;
            file.PageReg = txtPageReg.Text;
            file.Url = txtURL.Text;
            file.txtURLReg = txtURLReg.Text;
            file.IconUrl = IconUrl.Text;
            file.TitleReg = txtTitleReg.Text;
            file.RootPath = txtrootPath.Text;
            file.PageUrl = txtPageUrl.Text;
            file.PageNumber = int.Parse(txtPageNumber.Text);
            file.ClearReg = txtClearReg.Text;
            file.CategorieID = int.Parse(txtCategorieID.Text);
            file.DescReg = txtDescReg.Text;
            BLL.FilesConfig.SaveFilesConfig(file, file.FileName);
        }
    }
}
